
$(function () {
    // 获取图片上传路径
//    var uploadPath = getUploadPath();
    $("#sta_id").hide();
    var image_id = $("#sta_id").text();
    var type;
    if (image_id == undefined || image_id == "" || image_id == null || image_id == 0) {
        // 添加广告
        type = 1
    } else {
        // 编辑广告
        type = 2;
    }

//    $("#img_upload").fileinput({
//        showUpload: false,
//        showCaption: false,
//        showPreview: false,
//        browseClass: "btn btn-primary btn-lg",
//        fileType: "any"
////        previewFileIcon: "<i class='glyphicon glyphicon-king'></i>"
////        allowedFileExtensions: ['jpg', 'png','gif']
//    });

    $("#sta_save").click(function () {
        var Name = $("#sta_name").val().trim();
        var clickUrl = $("#sta_click").val().trim();
        var fileArr = $("#img_upload").val().split('\\');
        var fileName = fileArr[fileArr.length - 1];
        var id = $("#sta_id").html();
        picUpload(Name, clickUrl, type, fileName, id);
    });
    var uploadUrl = "http://localhost:8080/itmaDevice/addOrupdateStartImages.do";
    $("#img_upload").fileinput({
        language: 'zh', //设置语言
        uploadUrl: uploadUrl, //上传的地址
        allowedFileExtensions : ['jpg', 'png','gif'],//接收的文件后缀
        showUpload: true, //是否显示上传按钮
        showCaption: false,//是否显示标题
        browseClass: "btn btn-primary", //按钮样式
        previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
        uploadAsync: false,
        uploadExtraData:function (previewId, index) {
            var obj = {};
            $('#addForm').find('input').each(function() {
                var id = $(this).attr('id'), val = $(this).val();
                obj[id] = val;
            });
            return obj;
        }
    });
});
function picUpload(Name, clickUrl, type, fileName, id) {
    $.ajaxFileUpload({
        url : 'addOrupdateStartImages.do?fileName=' + fileName + '&type=' + type + '&name=' + Name + '&id='+ id + '&clickUrl=' + clickUrl + '&nowTime=' + new Date().getTime(),
//        url : 'adPicUpload.do',
        secureuri : false,
        fileElementId : 'img_upload',
        dataType : 'json',
        success : function(data) {
            if (data["code"] == "1000") {
                layer.msg('保存成功!', {icon: 1, time: 1000});
                window.setTimeout("eval(1);",1000);
                window.parent.location.reload();
            } else {
                layer.msg('保存失败!', {icon: 5, time: 1000});
            }

        },
        error : function(data) {
            layer.msg('操作失败!', {icon: 2, time: 1000});
        }
    });
}
function getUploadPath () {
    $.ajax({
        url: "adDelete.do",
        type: "post",
        async: true,
        data:"id=" + id,
        dataType: "json",
        success: function (data) {
            if (data["code"] == "1000") {
                location.replace(location.href);
//                $(obj).parents("tr").remove();
//                layer.msg('已删除!', {icon: 1, time: 1000});
            } else {
                layer.msg('删除失败!', {icon: 5, time: 1000});
            }
        },
        error: function (data) {
            layer.msg('操作失败!', {icon: 2, time: 1000});
        }
    });
}